package logic

import (
	"cart/mysql"
	"context"

	"cart/cart"
	"cart/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CartUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCartUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartUpdateLogic {
	return &CartUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CartUpdateLogic) CartUpdate(in *cart.CartUpdateRequest) (*cart.CartUpdateResponse, error) {
	// todo: 修改购物车商品数量
	var err error
	var g mysql.Goods
	err = g.Finds(int(in.GoodsId))
	if err != nil {
		logx.Error("没有此商品，", err)
		return nil, err
	}

	g.Stock = int(in.GoodsNum)

	err = g.Update(int(in.GoodsNum))
	if err != nil {
		logx.Error("修改失败", err)
		return nil, err
	}

	return &cart.CartUpdateResponse{
		Id: int64(g.ID),
	}, nil
}
